﻿using System;
using System.Windows.Input;

namespace AccessControl.Wpf.Commands
{
	class CommandBase : ICommand
	{
		#region Fields

		Action _actionToExecute = null;

		#endregion

		#region Public Constructor

		/// <summary>
		/// Initializes a new instance of the <see cref="CommandBase"/> class.
		/// </summary>
		/// <param name="actionToExecute">The action to execute.</param>
		/// <remarks></remarks>
		public CommandBase(Action actionToExecute)
		{
			_actionToExecute = actionToExecute;
		}

		#endregion

		#region ICommand Members

		/// <summary>
		/// Defines the method that determines whether the command can execute in its current state.
		/// </summary>
		/// <param name="parameter">Data used by the command.  If the command does not require data to be passed, this object can be set to null.</param>
		/// <returns>true if this command can be executed; otherwise, false.</returns>
		/// <remarks></remarks>
		public virtual bool CanExecute(object parameter)
		{
			return (_actionToExecute != null);
		}

		public event EventHandler CanExecuteChanged;

		/// <summary>
		/// Defines the method to be called when the command is invoked.
		/// </summary>
		/// <param name="parameter">Data used by the command.  If the command does not require data to be passed, this object can be set to null.</param>
		/// <remarks></remarks>
		public virtual void Execute(object parameter)
		{
			_actionToExecute.Invoke();
		}

		#endregion
	}
}
